package com.atguigu.controller;

import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @title：
 * @author：Yu
 * @create：2023-02-10 17:18
 * @description：
 */
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    @DubboReference
    private HouseService houseService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private DictService dictService;
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseUserService houseUserService;

    @RequestMapping()
    public String findPage(Map map, HttpServletRequest request) {
        Map<String, Object> filters = getFilters(request);
        map.put("filters", filters);
        PageInfo<House> page = houseService.findPage(filters);
        map.put("page", page);
        getValues(map);
        return "house/index";
    }

    @RequestMapping("/create")
    public String create(Map map) {
        getValues(map);
        return "house/create";
    }

    @RequestMapping("/save")
    public String save(House house) {
        houseService.insert(house);
        return "common/success";
    }

    public void getValues(Map map) {
        List<Community> communityList = communityService.findAll();
        List<Dict> houseTypeList = dictService.findById(10000L);
        List<Dict> floorList = dictService.findById(20000L);
        List<Dict> buildStructureList = dictService.findById(30000L);
        List<Dict> decorationList = dictService.findById(40000L);
        List<Dict> directionList = dictService.findById(50000L);
        List<Dict> houseUseList = dictService.findById(60000L);
        map.put("communityList", communityList);
        map.put("houseTypeList", houseTypeList);
        map.put("floorList", floorList);
        map.put("buildStructureList", buildStructureList);
        map.put("directionList", directionList);
        map.put("decorationList", decorationList);
        map.put("houseUseList", houseUseList);
    }

    @RequestMapping("/edit/{id}")
    public String edit(Map map, @PathVariable Long id) {
        House house = houseService.findById(id);
        map.put("house", house);
        getValues(map);
        return "house/edit";
    }

    @RequestMapping("/update")
    public String update(House house) {
        houseService.update(house);
        return "common/success";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        houseService.delete(id);
        return "common/success";
    }

    @RequestMapping("/publish/{id}/{status}")
    public String publish(@PathVariable Long id, @PathVariable Integer status) {
        houseService.publish(id, status);
        return "redirect:/house";
    }

    @RequestMapping("/show/{id}")
    public String show(Map map, @PathVariable Long id) {
        Community community = communityService.findById(id);
        House house = houseService.findById(id);
        map.put("house", house);
        map.put("community", community);
        List<HouseImage> image1 = houseImageService.findImage(id, 1);
        List<HouseImage> image2 = houseImageService.findImage(id, 2);
        map.put("houseImage1List", image1);
        map.put("houseImage2List", image2);
        List<HouseBroker> broker = houseBrokerService.findBroker(id);
        map.put("houseBrokerList", broker);
        List<HouseUser> userByHouseId = houseUserService.findUserByHouseId(id);
        map.put("houseUserList", userByHouseId);
        return "house/show";
    }

}
